package com.hmdp.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hmdp.dto.Result;
import com.hmdp.dto.UserDTO;
import com.hmdp.entity.Blog;
import com.hmdp.entity.User;
import com.hmdp.service.IBlogService;
import com.hmdp.service.IUserService;
import com.hmdp.utils.SystemConstants;
import com.hmdp.utils.UserHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 */
@RestController
@RequestMapping("/blog")
@Slf4j
public class BlogController {

    @Resource
    private IBlogService blogService;


    /**
     * 发布探店笔记
     *
     * @param blog
     * @return
     */
    @PostMapping
    public Result saveBlog(@RequestBody Blog blog) {
        return blogService.saveBlog(blog);
    }

    /**
     * 点赞功能
     *
     * @param id
     * @return
     */
    @PutMapping("/like/{id}")
    public Result likeBlog(@PathVariable("id") Long id) {
        // 修改点赞数量
        return blogService.updateIsLike(id);
    }

    @GetMapping("/of/me")
    public Result queryMyBlog(@RequestParam(value = "current", defaultValue = "1") Integer current) {
        // 获取登录用户
        UserDTO user = UserHolder.getUser();
        // 根据用户查询
        Page<Blog> page = blogService.query()
                .eq("user_id", user.getId()).page(new Page<>(current, SystemConstants.MAX_PAGE_SIZE));
        // 获取当前页数据
        List<Blog> records = page.getRecords();
        return Result.success(records);
    }

    /**
     * 查询所有热点笔记
     */
    @GetMapping("/hot")
    public Result queryHotBlog(@RequestParam(value = "current", defaultValue = "1") Integer current) {
        return blogService.queryHostBlog(current);
    }

    /**
     * 查询单条热点笔记
     */
    @GetMapping("/{id}")
    public Result queryBlogById(@PathVariable Long id) {
        log.info("查询探店笔记，笔记id为:{}", id);
        return blogService.queryBlogById(id);
    }

    /**
     * 查询笔记点赞排行前五
     *
     * @param id
     * @return
     */
    @GetMapping("/likes/{id}")
    public Result queryBlogLikesTop5(@PathVariable Long id) {
        log.info("查询探店点赞排行榜，笔记id为:{}", id);
        return blogService.queryBlogLikesTop5(id);
    }

    /**
     * 根据userId查询笔记信息
     *
     * @param current
     * @param id
     * @return
     */
    @GetMapping("/of/user")
    public Result queryBlogByUserId(
            @RequestParam(value = "current", defaultValue = "1") Integer current,
            @RequestParam("id") Long id) {
        // 根据用户查询
        Page<Blog> page = blogService.query()
                .eq("user_id", id).page(new Page<>(current, SystemConstants.MAX_PAGE_SIZE));
        // 获取当前页数据
        List<Blog> records = page.getRecords();
        return Result.success(records);
    }

    /**
     * 滚动查询推送的blog
     * @param maxTime 上次查询的最小时间戳 - 这次查询的最大时间戳
     * @param minTimeCount 和上次查询最小时间戳相等的个数 - 偏移量
     * @return
     */
    @GetMapping("/of/follow")
    public Result queryBlogOfFollows(
            @RequestParam(value = "lastId") Long maxTime,
            @RequestParam(value = "offset", defaultValue = "0") Long minTimeCount
    ){
        log.info("开始滚动查询推送笔记，最小时间戳为：{} 偏移量为：{}", maxTime, minTimeCount);
        return blogService.queryBlogOfFollows(maxTime, minTimeCount);
    }
}
